package yamlApply

import (
	"context"
	"gitee.com/Frank098/devops-app-mgt/model"
	"gitee.com/Frank098/devops-app-mgt/service"
	corev1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func CreatePod(name, ns string, label map[string]string, spec corev1.PodSpec) (*corev1.Pod, error) {
	pod := corev1.Pod{
		TypeMeta: metav1.TypeMeta{
			Kind:       "Pod",
			APIVersion: "v1",
		},
		ObjectMeta: metav1.ObjectMeta{
			Name:      name,
			Namespace: ns,
			Labels:    label,
		},
		Spec: spec,
	}
	create, err := service.K8s.ClientSet.CoreV1().Pods(ns).Create(context.TODO(), &pod, metav1.CreateOptions{})
	if err != nil {
		return nil, err
	}
	return create, nil
}

func UpdatePod(pod *corev1.Pod) (*model.Response, error) {
	_, err := service.K8s.ClientSet.CoreV1().Pods(pod.Name).Update(context.TODO(), pod, metav1.UpdateOptions{})
	if err != nil {
		return nil, err
	}
	return &model.OK, nil
}
